package com.wy.service.impl.wy;

import com.netsdk.apis.DeviceAPI;
import com.wy.dao.DeviceEntityMapper;
import com.wy.model.sys.DeviceEntity;
import com.wy.service.wy.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("deviceServiceImpl")
public class DeviceServiceImpl implements DeviceService {

	@Autowired
	private DeviceEntityMapper deviceEntityMapper;

	@Override
	public List<DeviceEntity> deviceList(Integer pageSize, Integer start, Integer residenceId, String deviceName,List<String> rids) {
		return deviceEntityMapper.deviceList(pageSize,start,residenceId,deviceName,rids);
	}

	@Override
	public Integer deviceSize(Integer pageSize, Integer start,Integer residenceId,String deviceName,List<String> rids) {
		return deviceEntityMapper.deviceSize(pageSize,start,residenceId,deviceName,rids);
	}

	@Override
	public void insertDevice(DeviceEntity deviceEntity) {
		deviceEntityMapper.insert(deviceEntity);
	}

	@Override
	public void updateDevice(DeviceEntity deviceEntity) {
		deviceEntityMapper.updateByPrimaryKey(deviceEntity);
	}

	@Override
	public void deleteDevices(Integer hid) {
		deviceEntityMapper.deleteByPrimaryKey(hid);
	}

	@Override
	public void deleteDevices(List<String> groupId) {
		deviceEntityMapper.deleteDevices(groupId);
	}

	@Override
	public void insertBatch(List<DeviceEntity> list) {
		deviceEntityMapper.insertBatch(list);
	}

	@Override
	public List<DeviceEntity> getDeviceByName(Integer residenceId,String deviceName) {
		return deviceEntityMapper.getDeviceByName(residenceId,deviceName);
	}

	@Override
	public List<DeviceEntity> getDeviceByAddress(Integer residenceId,String deviceAddress,String deviceChannelId) {
		return deviceEntityMapper.getDeviceByAddress(residenceId,deviceAddress,deviceChannelId);
	}

	@Override
	public List<DeviceEntity> getDeviceByRidAndType(Integer rid,Integer type) {
		return deviceEntityMapper.getDeviceByRidAndType(rid,type);
	}

	@Override
	public boolean openDoor(Integer id) {
		DeviceEntity deviceEntity = deviceEntityMapper.selectByPrimaryKey(id);
		DeviceAPI deviceAPI = new DeviceAPI();
		return deviceAPI.openDoor(deviceEntity.getDeviceAddress(),deviceEntity.getDevicePort(),deviceEntity.getDeviceUsername(),deviceEntity.getDevicePassword(),new Short(deviceEntity.getDeviceChannelId()));
	}


}
